<template>
    <div class="main-categories-index">
        <van-search v-model="keyword" placeholder="Search" />
        <section>
            <div class="warp">
                <van-row gutter="22">
                    <van-col span="8" v-for="(item, index) in categories" :key="index">
                        <router-link :to="{name: 'CategoriesInfo', query: {cid: item.cid}}" class="categories-list" >
                            <van-image
                                class="img"
                                round
                                :src="item.categories_img"
                            />
                            <div class="categories_name">{{item.categories_name}}</div>
                        </router-link>
                    </van-col>
                </van-row>
            </div>
        </section>
    </div>
</template>

<script lang="ts" setup>
import { Toast } from 'vant'
import { onMounted, ref } from 'vue'
import { GetCategorieList } from '../../api/index'

interface Categories {
    categories_img: string;
    categories_name: string;
    cid: number;
    url: {name: string; query?:{}}
}

const keyword = ref('')

const categories = ref<Array<Categories>>([])

const getCategorieList = () =>{
    const toast = Toast.loading({
            message: '加载中...',
            forbidClick: true,
        });
    GetCategorieList({}).then((res: any) => {
        categories.value = res.data.map((row: Categories) => {
            return {
                ...row, 
                 url: {
                    name: 'CategoriesInfo',
                    query: {
                        cid: row.cid
                    }
                }
            }
        })
        toast.clear()
    })
}

onMounted(() => {
    getCategorieList()
})

</script>

<style lang="less" scoped>
.main-categories-index {
    section {
        .warp {
            margin: 10px 12px;
            .categories-list{
                display: flex;
                flex: 1;
                flex-direction: column;
                justify-items: center;
                align-items: center;
                margin-bottom: 30px;
                .img {
                    width: 100%;
                }
                .categories_name {
                    margin-top: 20px;
                    font-size: 15px;
                    line-height: 18px;
                    color: #6D3805;
                }
            }
        }
    }
}
</style>
